// 主题切换
const themeToggle = document.getElementById('themeToggle');
themeToggle.addEventListener('click', () => {
    document.body.dataset.theme = document.body.dataset.theme === 'dark' ? 'light' : 'dark';
});

// 番茄钟相关
class PomodoroTimer {
    constructor() {
        this.timeLeft = 25 * 60; // 25分钟
        this.isRunning = false;
        this.timer = null;
        this.completedPomodoros = 0;
    }

    start() {
        if (!this.isRunning) {
            this.isRunning = true;
            this.timer = setInterval(() => this.tick(), 1000);
        }
    }

    pause() {
        if (this.isRunning) {
            this.isRunning = false;
            clearInterval(this.timer);
        }
    }

    reset() {
        this.pause();
        this.timeLeft = 25 * 60;
        this.updateDisplay();
    }

    tick() {
        if (this.timeLeft > 0) {
            this.timeLeft--;
            this.updateDisplay();
        } else {
            this.completePomodoro();
        }
    }

    updateDisplay() {
        const minutes = Math.floor(this.timeLeft / 60);
        const seconds = this.timeLeft % 60;
        const display = `${minutes.toString().padStart(2, '0')}:${seconds.toString().padStart(2, '0')}`;
        document.getElementById('timer').textContent = display;
    }

    completePomodoro() {
        this.pause();
        this.completedPomodoros++;
        document.getElementById('completedPomodoros').textContent = this.completedPomodoros;
        // 可以在这里添加提醒声音
        alert('番茄钟完成！');
        this.reset();
    }
}

// 初始化番茄钟
const pomodoro = new PomodoroTimer();

// 绑定按钮事件
document.getElementById('startTimer').addEventListener('click', () => pomodoro.start());
document.getElementById('pauseTimer').addEventListener('click', () => pomodoro.pause());
document.getElementById('resetTimer').addEventListener('click', () => pomodoro.reset());

// 任务列表相关
class TaskManager {
    constructor() {
        this.tasks = [];
    }

    addTask(title, estimatedPomodoros = 1) {
        const task = {
            id: Date.now(),
            title,
            completed: false,
            estimatedPomodoros,
        };
        this.tasks.push(task);
        this.renderTasks();
    }

    toggleTask(id) {
        const task = this.tasks.find(t => t.id === id);
        if (task) {
            task.completed = !task.completed;
            this.renderTasks();
        }
    }

    deleteTask(id) {
        this.tasks = this.tasks.filter(t => t.id !== id);
        this.renderTasks();
    }

    renderTasks() {
        const tasksList = document.getElementById('tasksList');
        tasksList.innerHTML = '';
        
        this.tasks.forEach(task => {
            const taskElement = document.createElement('div');
            taskElement.className = 'task-item';
            taskElement.innerHTML = `
                <input type="checkbox" ${task.completed ? 'checked' : ''}>
                <span>${task.title}</span>
                <span>(${task.estimatedPomodoros} 番茄钟)</span>
                <button onclick="taskManager.deleteTask(${task.id})">删除</button>
            `;
            
            const checkbox = taskElement.querySelector('input');
            checkbox.addEventListener('change', () => this.toggleTask(task.id));
            
            tasksList.appendChild(taskElement);
        });
    }
}

// 初始化任务管理器
const taskManager = new TaskManager();

// 添加任务按钮事件
document.getElementById('addTask').addEventListener('click', () => {
    const title = prompt('请输入任务名称：');
    if (title) {
        const estimatedPomodoros = parseInt(prompt('预计需要几个番茄钟？', '1')) || 1;
        taskManager.addTask(title, estimatedPomodoros);
    }
});

// 白噪音相关
class SoundManager {
    constructor() {
        this.sounds = {};
        this.initializeSounds();
    }

    initializeSounds() {
        const soundItems = document.querySelectorAll('.sound-item');
        soundItems.forEach(item => {
            const label = item.querySelector('label').textContent;
            const slider = item.querySelector('.volume-slider');
            const button = item.querySelector('.play-btn');
            
            // 这里可以添加实际的音频文件
            const audio = new Audio();
            audio.loop = true;
            
            this.sounds[label] = {
                audio,
                slider,
                button,
                playing: false
            };
            
            button.addEventListener('click', () => this.toggleSound(label));
            slider.addEventListener('input', (e) => this.updateVolume(label, e.target.value));
        });
    }

    toggleSound(label) {
        const sound = this.sounds[label];
        if (sound.playing) {
            sound.audio.pause();
            sound.button.textContent = '播放';
        } else {
            sound.audio.play();
            sound.button.textContent = '暂停';
        }
        sound.playing = !sound.playing;
    }

    updateVolume(label, value) {
        const sound = this.sounds[label];
        sound.audio.volume = value / 100;
    }
}

// 初始化声音管理器
const soundManager = new SoundManager();
